var ConfirmWindownMap = new Object();
var ConfirmWindown =  function(opt){
	this.target = opt.target?opt.target:'confirm';
	this.content = opt.content?opt.content:'pConfirmBox',
	this.yesText = opt.yes?opt.yes:'Yes';
	this.noText = opt.no?opt.no:'No',
	this.canReturn = opt.canReturn?opt.canReturn:false,
	this.titleText = opt.titleText?opt.titleText:'Warn',
	this.focusClassNo = opt.focusClass?opt.focusClass:'_confirmBoxCurNoBtn';
	this.normalClassNo = opt.normalClass?opt.normalClass:'_confirmBoxNoBtn';
	this.focusClassYes = opt.focusClass?opt.focusClass:'_confirmBoxCurYesBtn';
	this.normalClassYes = opt.normalClass?opt.normalClass:'_confirmBoxYesBtn';
	
	this.onclickYes = false;
	this.onclickNo = false;
	this.hasInit = false;
	this.onHide = false;

	ConfirmWindownMap[this.target] = this;
}

ConfirmWindown.prototype = {
	changeText:function(no,yes){
		if(!this.hasInit){
			this.init();
			this.hasInit = true;
		}
		var yesA = document.getElementById(this.target+'-a-yes');
		yesA.innerHTML = yes;
		var noA = document.getElementById(this.target+'-a-no');
		noA.innerHTML = no;
	},
	show:function(tip,onclickNo,onclickYes,onHide,left,top){
		if(!this.hasInit){
			this.init();
			this.hasInit = true;
		}
		this.onclickYes = onclickYes;
		this.onclickNo = onclickNo;
		this.onHide = onHide;
		
		var yesA = document.getElementById(this.target+'-a-yes');
		yesA.parentNode.className = this.normalClass;
		var noA = document.getElementById(this.target+'-a-no');
		noA.parentNode.className = this.normalClass;
		
		var con = document.getElementById(this.content); 
		var child = con.childNodes;
		var tipBox = child[1];
		tipBox.innerHTML = tip;
		
		con.style.display = 'block';
		if(left){
			con.style.left = left;
		}
		if(top){
			con.style.top = top;
		}
		noA.focus();
	},
	hide:function(){
		var con = document.getElementById(this.content);
		con.style.display = 'none';
		
		KEY.CURTYPE = false;
		if(this.onHide){
			this.onHide();
		}
	},
	init:function(){
		var con = document.getElementById(this.content); 
		var child = con.childNodes;
		
		var titleBox = child[0];
		titleBox.innerHTML = this.titleText;
		
		var btnBox = child[child.length - 1];
		
		var html = '<div class="'+this.normalClassNo+'"><a id="'+this.target+'-a-no" href="#">no</a></div><div class="'+this.normalClassYes+'"><a id="'+this.target+'-a-yes" href="#">yes</a></div>';
		btnBox.innerHTML = html;
		var aList = btnBox.getElementsByTagName('a');
		var a;
		for(var i = 0;i<aList.length;i++){
			a = aList[i];
			a.onfocus = this.focusItem;
			a.onblur = this.blurItem;
			a.onclick = this.clickItem;
		}
	},
	focusItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var id = a.id;
		var idArray = id.split('-'); 
		var type = idArray[2];
		var _this = ConfirmWindownMap[idArray[0]];
		if(type == 'no'){
			par.className = _this.focusClassNo; 
		}else{
			par.className = _this.focusClassYes; 
		}
		
		KEY.CURTYPE = 'confirmBox';
	},
	blurItem:function(event){
		var a = event.currentTarget;
		var par = a.parentNode;
		var id = a.id;
		var idArray = id.split('-');
		var _this = ConfirmWindownMap[idArray[0]];
		var type = idArray[2];
		if(type == 'no'){
			par.className = _this.normalClassNo; 
		}else{
			par.className = _this.normalClassYes; 
		}
		
	},
	clickItem:function(event){
		var a = event.currentTarget;
		var id = a.id;
		var idArray = id.split('-');
		var _this = ConfirmWindownMap[idArray[0]];
		var tar = idArray[2];
		
		
		if(tar == 'yes'){
			if(_this.onclickYes){
				_this.onclickYes();
			}
		}else if(tar == 'no'){
			if(_this.onclickNo){
				_this.onclickNo();
			}
		}
		_this.hide();
	}
}

KEY.TYPE['confirmBox'] = keyRecvConfirm;

function keyRecvConfirm(key){
	var a = document.activeElement;
	var id = a.id;
	var idArray = id.split('-');
	var _this = ConfirmWindownMap[idArray[0]];
	if(key == 0x11 && _this.canReturn){		//返回
		_this.hide();
	}
	return true;
}